﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace docmgr.WinForm
{
    public partial class AddPositionAuthority : Form
    {

        Object id = null;

        ArrayList list = new ArrayList();
        public AddPositionAuthority()
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

        }

        public AddPositionAuthority(Object key)
        {
            InitializeComponent();
            this.id = key;
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String sqlStr = "delete [PositionAothority] where PositionID=" + Convert.ToInt32(id);

            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr);
            ArrayList tarray=new ArrayList();
            for(int i=0;i<dataGridView1.Rows.Count;i++)
            {
                 if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["checkbox"].Value) == true)
                 {
                     tarray.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells["ColumnID"].Value));
                 }
            }
            sqlStr="";
            if(tarray.Count==0)
            {
                this.Close();
                return;
            }
            sqlStr = "insert [PositionAothority] (PositionID,AuthorityID) values ";

            for(int i=0;i<tarray.Count;i++)
            {
                if (i != 0)
                    sqlStr += " , ";

                sqlStr += String.Format(" ( {0},{1}) ", Convert.ToInt32(id), tarray[i]);
            }
            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr);

            this.Close();
            return;

        }

        private void AddPositionAuthority_Load(object sender, EventArgs e)
        {
            String sqlStr = "SELECT a.ID  from [PositionAothority] as pa LEFT JOIN [Authority] as a on pa.AuthorityID=a.ID  where pa.PositionID="+Convert.ToInt32(id);

            DataTable dt = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];

            for(int i=0;i<dt.Rows.Count;i++)
            {
                list.Add(Convert.ToInt32(dt.Rows[i]["ID"]));
            }
            String sqls = "SELECT ID,Name from [Authority]";
            dataGridView1.DataSource = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqls).Tables[0];


            for(int i=0;i<dataGridView1.Rows.Count;i++)
            {
                int tp = Convert.ToInt32( dataGridView1.Rows[i].Cells["ColumnID"].Value);

                if(list.Contains(tp))
                {
                    dataGridView1.Rows[i].Cells["checkbox"].Value = true;
                }
            }


        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value) != true)
                    dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value = true;
                else
                    dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value = false;

            }
        }
    }
}
